package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.chat.Friendships;
import com.mask.im.business.model.entity.TransferHistory;
import com.mask.im.business.mapper.FriendshipsMapper;
import com.mask.im.business.mapper.TransferHistoryMapper;
import com.mask.im.business.mapper.UserInfoMapper;
import com.mask.im.business.model.dto.TransferHistoryMsgDTO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.TransferHistoryService;
import com.mask.im.business.common.util.BeanCopy;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Service
public class TransferHistoryServiceImpl extends ServiceImpl<TransferHistoryMapper, TransferHistory> implements TransferHistoryService {

	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private FriendshipsMapper friendshipsMapper;
	@Override
	public RestResponse<TransferHistoryMsgDTO> transferHistoryById(Integer id) {
		Integer userId = MaskIMSecurityUtils.getUserId();
		TransferHistory transferHistory = this.getById(id);
		TransferHistoryMsgDTO transferHistoryMsgDTO = BeanCopy.singleCopy(transferHistory, TransferHistoryMsgDTO.class);
		BigDecimal transferAmount = transferHistory.getTransferAmount();
		transferHistoryMsgDTO.setTransferAmount(transferAmount.setScale(2, RoundingMode.HALF_UP).toString());
		Long acceptUserId = transferHistory.getAcceptUserId();
		Long transferUserId = transferHistory.getTransferUserId();
		if (userId.equals(acceptUserId)){
			Friendships friendships = friendshipsMapper.selectOne(new LambdaQueryWrapper<Friendships>()
					.eq(Friendships::getOneUserId, transferUserId)
					.eq(Friendships::getTwoUserId, userId));
			transferHistoryMsgDTO.setIllustrate(friendships.getRemark()+"给你转账一笔金额");
		}else{
			Friendships friendships = friendshipsMapper.selectOne(new LambdaQueryWrapper<Friendships>()
					.eq(Friendships::getOneUserId, userId)
					.eq(Friendships::getTwoUserId, acceptUserId));
			transferHistoryMsgDTO.setIllustrate("转账给"+friendships.getRemark());
		}
		return RestResponse.ok(transferHistoryMsgDTO);
	}
}
